package com.gz.springcloud.service.impl;

import com.gz.springcloud.dao.OrderDao;
import com.gz.springcloud.entity.Order;
import com.gz.springcloud.service.AccountService;
import com.gz.springcloud.service.OrderService;
import com.gz.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;

    @Resource
    private StorageService storageService;

    @Resource
    private AccountService accountService;


    @Override
    public void save(Order order) {

        log.info("====创建订单 start=====");

        //调用本地方法生成订单order
        log.info("====本地生成订单 start===");
        orderDao.save(order);
        log.info("====本地生成订单 end===");

        //远程调用storage微服务扣减库存
        log.info("====扣减库存 start===");
        storageService.reduce(order.getProductId(), order.getNums());
        log.info("====扣减库存 end===");

        //远程调用account微服务扣减用户money
        log.info("====扣减用户余额 start===");
        accountService.reduce(order.getUserId(), order.getMoney());
        log.info("====扣减用户余额 end===");

        //调用本地方法修改订单状态0->1  '0：创建中; 1：已完结'
        log.info("====本地修改订单状态 start===");
        orderDao.update(order.getUserId(), 0);
        log.info("====本地修改订单状态 end===");

        log.info("====创建订单 end=====");
    }
}
